package com.family.web.controller.pedigree;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.family.common.core.controller.BaseController;
import com.family.common.core.domain.AjaxResult;
import com.family.common.core.page.TableDataInfo;
import com.family.common.utils.StringUtils;
import com.family.pedigree.domain.FamilyCompanyAssets;
import com.family.pedigree.service.IFamilyCompanyAssetsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 公司资产表 前端控制器
 * </p>
 *
 * @author forever
 * @since 2025-01-16
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/family/company/assets")
public class FamilyCompanyAssetsController extends BaseController {

    private final IFamilyCompanyAssetsService familyCompanyAssetsService;

    @PostMapping
    public AjaxResult add(@Validated @RequestBody FamilyCompanyAssets familyCompanyAssets) {
        return familyCompanyAssetsService.save(familyCompanyAssets) ? success() : error();
    }

    @PutMapping
    public AjaxResult edit(@Validated @RequestBody FamilyCompanyAssets familyCompanyAssets) {
        return familyCompanyAssetsService.updateById(familyCompanyAssets) ? success() : error();
    }

    @DeleteMapping("/{assetsId}")
    public AjaxResult delById(@PathVariable Long assetsId) {
        return familyCompanyAssetsService.removeById(assetsId) ? success() : error();
    }

    @GetMapping("/page")
    public TableDataInfo page(FamilyCompanyAssets familyCompanyAssets) {
        startPage();
        List<FamilyCompanyAssets> result = familyCompanyAssetsService.list(Wrappers.<FamilyCompanyAssets>lambdaQuery()
                .eq(Objects.nonNull(familyCompanyAssets.getCompanyId()), FamilyCompanyAssets::getCompanyId, familyCompanyAssets.getCompanyId())
                .eq(Objects.nonNull(familyCompanyAssets.getParticularYear()), FamilyCompanyAssets::getParticularYear, familyCompanyAssets.getParticularYear())
                .orderByDesc(FamilyCompanyAssets::getParticularYear));
        return getDataTable(result);
    }


}
